//
//  TimeTextLabel.swift
//  TimMusic
//
//  Created by nkl on 2024/10/25.
//

import Foundation
import SwiftUI

struct TimeTextLabel: View {
    enum Style: Int, CaseIterable {
        case all
        case hourAndMinute
        case minuteAndSecond

        case hour
        case minute
        case second
        case countdownHour
        case countdownMinute
        case countdownSecond

        case hourTen
        case hourSingle
        case minuteTen
        case minuteSingle
        case secondTen
        case secondSingle

        var isCountdown: Bool {
            return self == .countdownHour || self == .countdownMinute || self == .countdownSecond
        }
    }

    var style: Style = .hour

    var date: Date
    var font: UIFont
    var color: UIColor?
    var shadowColor: Color?

    var countdownDate: Date?

    var disableFilpAnimte: Bool = false
    var timeZone: TimeZone? = nil

    @Environment(\.layoutDirection) var direction

    var locale: Locale? = nil

    var body: some View {
        let numberText = 0.localNumber(locale: locale) ?? "0"
        let specText = ":"
        var frameWidthText = numberText + numberText

        var leadingText: String = numberText
        var trailText: String = numberText

        switch style {
        case .all:
            // "0"
            leadingText = numberText
            // ""
            trailText = ""
            // "00:00:00"
            frameWidthText = numberText + numberText + specText + numberText + numberText + specText + numberText + numberText
        case .hourAndMinute:
            // "0"
            leadingText = numberText
            // ":00"
            trailText = specText + numberText + numberText
            // "00:00"
            frameWidthText = numberText + numberText + specText + numberText + numberText
        case .minuteAndSecond:
            // "000:"
            leadingText = numberText + numberText + numberText + specText
            // ""
            trailText = ""
            // "00:00"
            frameWidthText = numberText + numberText + specText + numberText + numberText
        case .hour, .countdownHour:
            // "0"
            leadingText = numberText
            // ":00:00"
            trailText = specText + numberText + numberText + specText + numberText + numberText
        case .minute, .countdownMinute:
            // "000:"
            leadingText = numberText + numberText + numberText + specText
            // ":00"
            trailText = specText + numberText + numberText
        case .second, .countdownSecond:
            // "000:00:"
            leadingText = numberText + numberText + numberText + specText + numberText + numberText + specText
            // ""
            trailText = ""
        case .hourTen:
            // "0"
            leadingText = numberText
            // "0:00:00"
            trailText = numberText + specText + numberText + numberText + specText + numberText + numberText
            frameWidthText = numberText
        case .hourSingle:
            // "00"
            leadingText = numberText + numberText
            // "0:00:00"
            trailText = specText + numberText + numberText + specText + numberText + numberText
            frameWidthText = numberText
        case .minuteTen:
            // "100:"
            leadingText = numberText + numberText + numberText + specText
            // "0:00"
            trailText = numberText + specText + numberText + numberText
            frameWidthText = numberText
        case .minuteSingle:
            // "000:0"
            leadingText = numberText + numberText + numberText + specText + numberText
            // ":00"
            trailText = specText + numberText + numberText
            frameWidthText = numberText
        case .secondTen:
            // "100:00:"
            leadingText = numberText + numberText + numberText + specText + numberText + numberText + specText
            // "0"
            trailText = numberText
            frameWidthText = numberText
        case .secondSingle:
            // "000:00:0"
            leadingText = numberText + numberText + numberText + specText + numberText + numberText + specText + numberText
            // ""
            trailText = ""
            frameWidthText = numberText
        }
        let fullText = numberText + numberText + numberText + ":" + numberText + numberText + ":" + numberText + numberText

        let frameWidth = font.widthForWord(frameWidthText)
        let leading = font.widthForWord(leadingText)
        let trailing = font.widthForWord(trailText)

        var calendar = Calendar.current
        if let timeZone = timeZone {
            calendar.timeZone = timeZone
        }
        let start = calendar.startOfDay(for: date)

        var targetDate = start.addingTimeInterval(-100 * 60 * 60)
        if style.isCountdown {
            targetDate = start.addingTimeInterval(224 * 60 * 60)
            if let countdownDate = countdownDate {
                targetDate = countdownDate.addingTimeInterval(200 * 60 * 60)
            }
        }
        return HStack(alignment: .top, spacing: 0) {
            Text(targetDate, style: .timer)
                .foregroundColor(color?.toColor())
                .lineLimit(1)
                .font(Font(font))
                .frame(width: font.widthForWord(fullText), alignment: .trailing)
                .padding(.leading, direction == .leftToRight ? -leading : -trailing)
                .padding(.trailing, direction == .leftToRight ? -trailing : -leading)
                .disableFlipAnimate(disableFilpAnimte)
        }
//        .border(Color.yellow)
        .frame(width: frameWidth)
        .clipped()
    }
}
